/**
 * 统计
 */

class StatisticalController extends BasicController {

    async actionBuilding() {
        let today = new Date();
        let year = today.getFullYear();
        let month = today.getMonth() + 1;
        if (month < 10) {
            month = '0' + month;
        }
        let start_time = year + '-' + month + '-01' + ' 00:00:00'
        let allCount = await db.sql('select COUNT(id) as count_num from hz_building where deleted_time = :deleted_time', { deleted_time: '' });
        let monthCount = await db.sql('select COUNT(id) as count_num from hz_building where created_time >= :created_time and deleted_time = :deleted_time', { created_time: start_time, deleted_time: '' });
        return {
            all_count: allCount[0].count_num,
            month_count: monthCount[0].count_num,
        }
    }

    async actionCompany() {
        let today = new Date();
        let year = today.getFullYear();
        let month = today.getMonth() + 1;
        if (month < 10) {
            month = '0' + month;
        }
        let start_time = year + '-' + month + '-01' + ' 00:00:00'
        let allCount = await db.sql('select COUNT(id) as count_num from hz_company where deleted_time = :deleted_time', { deleted_time: '' });
        let monthCount = await db.sql('select COUNT(id) as count_num from hz_company where created_time >= :created_time and deleted_time = :deleted_time', { created_time: start_time, deleted_time: '' });
        return {
            all_count: allCount[0].count_num,
            month_count: monthCount[0].count_num,
        }
    }

    async actionUser() {
        let today = parseInt(new Date().getTime()/1000);
        let todayStr = date("Y-m-d", today) + ' 00:00:00';
        let yesterdayStr = date("Y-m-d", (today - 86400)) + ' 00:00:00';
        let allCount = await db.sql('select COUNT(id) as count_num from hz_user where deleted_time = :deleted_time', { deleted_time: '' });
        let yesterdayCount = await db.sql('select COUNT(id) as count_num from hz_user where created_time >= :yesterdayStr and created_time <= :todayStr and deleted_time = :deleted_time', { yesterdayStr, todayStr, deleted_time: '' });
        let todayList = await db.sql('select created_time from hz_user where created_time >= :todayStr', { todayStr });
        let todayCountList = [];
        for (var i = 0; i < 24; i++) {
            todayCountList[i] = 0;
            let timeStr = date("Y-m-d", today);
            let timeStr1 = timeStr + ' ';
            let timeStr2 = timeStr + ' ';
            if (i < 10) {
                timeStr1 += ('0' + i + ':00:00');
            } else {
                timeStr1 += (i + ':00:00');
            }
            if (i < 9) {
                timeStr2 += ('0' + (i + 1) + ':00:00');
            } else {
                timeStr2 += ((i + 1) + ':00:00');
            }
            for (var j = 0; j < todayList.length; j++) {
                if (todayList[j].created_time >= timeStr1 && todayList[j].created_time < timeStr2) {
                    todayCountList[i] ++;
                }
            }
        }
        return {
            all_count: allCount[0].count_num,
            yesterday_count: yesterdayCount[0].count_num,
            today_count_list: todayCountList,
        }
    }
}

export default StatisticalController;